package com.yanqu.road.server.cmd.activity.common;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.CommonActivityMgr;
import com.yanqu.road.server.manger.activity.CommonActivityRankMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;
import com.yanqu.road.server.pb.ActivityPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_COMMON_ACTIVITY_MEMBER_CONDITION_DATA, desc = "获取商会成员贡献值")
public class CommonActivityMemberConditionDataCmd extends AbstractCommand {

	private int activityId;

	private String unionUid;

	private int type;

	private UnionActivityConditionData unionActivityConditionData;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CommonActivityProto.CommonActivityConditionUnionMemberDataReqMsg reqMsg = CommonActivityProto.CommonActivityConditionUnionMemberDataReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		unionUid = reqMsg.getUnionUid();
		type = reqMsg.getType();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = CommonActivityMgr.getOpenCommonActivityInfo(activityId);
		if(null == activityInfo){
			ret = GameErrorCode.E_COMMON_ACTIVITY_NO_IN_TIME;
			return;
		}
		if(type != ActivityHelper.COMMON_ACTIVITY_CONDITION_TYPE_UNION){
			ret = GameErrorCode.E_COMMON_ACTIVITY_TYPE_NO_UNION_RANK;
			return;
		}
        unionActivityConditionData = CommonActivityRankMgr.getUnionActivityConditionData(activityId, unionUid);
		if(null == unionActivityConditionData){
		    ret = GameErrorCode.E_COMMON_ACTIVITY_NO_JOIN;
		    return;
        }
	}

	@Override
	public void sendData(GamePlayer player) {
        CommonActivityProto.CommonActivityConditionUnionMemberDataRespMsg.Builder respMsg = CommonActivityProto.CommonActivityConditionUnionMemberDataRespMsg.newBuilder();
        respMsg.setRet(ret);
        if(0 == ret){
            if(null != unionActivityConditionData) {
                ActivityProto.ActivityConditionUnionMemberDataListMsg.Builder dataMsg = ActivityPb.parseActivityConditionUnionMemberDataListMsg(
                        UnionActivityMgr.getUserActivityConditionDataList(unionActivityConditionData, CommonActivityRankMgr.getUserActivityConditionDataMap(activityId)));
                respMsg.setDataList(dataMsg);
            }
        }
        player.sendPacket(Protocol.U_COMMON_ACTIVITY_MEMBER_CONDITION_DATA, respMsg);
	}

}
